package dao.impl;


import dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl  implements IUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * jdbcTemplate 提供了大量的CRUD操作，这里就不意义测试了，有兴趣的自己去试一把
     */
    @Override
    public void saveUser() {
        /**
         * 无变量的插入 直接写sql就可以
         */
        jdbcTemplate.execute("insert into t_user(username, age) values('test1', '1')");

        /**
         * 有变量的插入要借助占位符
         */
        String name = "xxxx";
        int age = 100;
        String insertSql = "insert into t_user(username, age) values(?, ?)";
        jdbcTemplate.update(insertSql, name, age);

    }

    @Override
    public void updateUser() {
        /**
         * 更新同插入一致
         */
        String oldUsername = "xxxx";
        String newUsername = "xxxxxx";
        String updateSql = "update t_user set username = ? where username = ?";
        jdbcTemplate.update(updateSql, newUsername, oldUsername);
    }
}
